package cn.tedu.sp02.controller;

import cn.tedu.entity.Item;
import cn.tedu.service.ItemService;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

@Slf4j
@RestController
public class ItemController {
    @Resource
    private ItemService itemService;
    //获取订单的商品列表
    @GetMapping("/{orderId}")
    public JsonResult<List<Item>> getItem(@PathVariable String orderId) throws InterruptedException {
        List<Item> items =itemService.getItems(orderId);
        //随机的阻塞代码
        if (Math.random()<0.9){
            int t = new Random().nextInt(5000);//随机5秒内时长
            log.info("暂停："+t);
            Thread.sleep(t);
        }
        return  JsonResult.build().code(200).data(items);
    }
    //减少商品库存
    //@RequestBody 接收post请求的协议体数据
    @PostMapping("/decreaseNumber")
    public JsonResult<?> decreaseNumber(@RequestBody List<Item> items){
        itemService.decreaseNumber(items);
        return JsonResult.build().code(200).msg("减少库存成功");
    }
    @GetMapping("/favicon.ico")
    public void ico(){

    }

}
